package com.yanlight.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.yanlight.app.dal.entity.ApplyMoney;
import com.yanlight.app.service.ApplyMoneyService;
import com.yanlight.app.service.CheckCodeService;
import com.yanlight.exception.ErrorType;
import com.yanlight.support.http.Response;
import com.yanlight.util.IpUtil;
import com.yanlight.util.LogUtil;
import com.yanlight.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/apply")
public class ApplyController {

    @Autowired
    ApplyMoneyService applyMoneyService;

    @Autowired
    CheckCodeService checkCodeService;

    /**
     * 申请贷款信息
     *
     * @return
     */
    @PostMapping("/money")
    @ResponseBody
    public Response money(HttpServletRequest request, ApplyMoney applyMoney, String content) {
        String ip = IpUtil.getIP(request);
        applyMoney.setIp(ip);

        ErrorType errorType = checkCodeService.verifyCode(applyMoney.getTel(), applyMoney.getType(), ip);
        if (errorType != ErrorType.SUCCESS) {
            return ResponseUtil.fail(errorType);
        }

        JSONObject jsonObject = JSONObject.parseObject(content);
        //获取申请金额
        if (jsonObject.containsKey("dkje")) {
            applyMoney.setQuota(jsonObject.getString("dkje"));
        }
        Response save = applyMoneyService.save(applyMoney);
        return save;
    }

    /**
     * 完善贷款信息
     *
     * @param id
     * @param houseInfo  房产情况
     * @param carInfo    车产情况
     * @param insureInfo 保险情况
     * @param fundInfo   公积金情况
     * @return
     */
    @PostMapping("/perfect")
    @ResponseBody
    public Response perfect(Long id, String houseInfo, String carInfo, String insureInfo, String fundInfo) {
        LogUtil.showLog(houseInfo);
        return ResponseUtil.success();
    }

    @PostMapping("/info")
    @ResponseBody
    public Response info(Integer applyId) {

        if (applyId == null) {
            return ResponseUtil.fail(ErrorType.UNLAWFUL_PARAMETERS);
        }

        Response response = applyMoneyService.queryById(applyId);
        return response;
    }

}
